package core.icq;

import core.Main;
import core.GlobalConfig;
import java.util.Arrays;
import java.util.Date;
import java.util.Vector;
import ru.caffeineim.protocols.icq.integration.OscarInterface;
import ru.caffeineim.protocols.icq.setting.enumerations.StatusModeEnum;
import tools.Logger;

/**
 *
 * @author dcrm
 */
public class FrameOfMind implements Runnable {

    private CoreICQ CORE;
    private Vector TIME = new Vector(Arrays.asList(Main.TIME));
    private Vector TIME_FREE = new Vector(Arrays.asList(Main.TIME_FREE));
    private Vector TIME_NIGHT = new Vector(Arrays.asList(Main.TIME_NIGHT));
    private Thread thread;
    private final String THREAD_NAME ="FrameOfMind";

    public FrameOfMind(CoreICQ core) {
        this.CORE = core;
        thread = new Thread(this);
        thread.setName(THREAD_NAME);
        thread.start();
    }

    private void setStatus(StatusModeEnum st) {
        OscarInterface.changeStatus(CORE.connection, st);
    }

    public void run() {
        try {
            Date dt;
            String dts;
            while(CORE.connection.isAuthorized()) {
                if (GlobalConfig.F_OF_MD) {
                    System.out.println("Time?");
                    dt = new Date();
                    dts = "" + dt.getHours() + ":" + dt.getMinutes() + "";
                    if (dt.getDay()<6)
                    {
                        if (TIME.contains(dts)) { setStatus(new StatusModeEnum(StatusModeEnum.OCCUPIED));}
                        if (TIME_FREE.contains(dts)) { setStatus(new StatusModeEnum(StatusModeEnum.FREE_FOR_CHAT)); }
                    }
                    if (dt.getDay()==7) if (TIME_FREE.contains("7:00")) { setStatus(new StatusModeEnum(StatusModeEnum.HOME)); }
                    if (TIME_NIGHT.contains(dts)) { setStatus(new StatusModeEnum(StatusModeEnum.FREE_FOR_CHAT)); } }
                    Thread.sleep(60000);
            } 
        } catch (Exception ex) {Logger.writelnErr(ex.getMessage()); }
    }
}
